﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Model.Database;
using SearchModel;

namespace Bll
{
    public partial class DataDictionaryItemBll
    {
        /// <summary>
        /// 条件分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="smDataDictionaryItem"></param>
        /// <returns></returns>
        public List<DataDictionaryItem> GetListBySearchPage(int pageIndex, int pageSize, out int total, SMDataDictionaryItem smDataDictionaryItem)
        {
            IQueryable<DataDictionaryItem> dataDictionaryItemIq = CurrentDal.GetIQueryable();

            dataDictionaryItemIq = BaseSearch(dataDictionaryItemIq, smDataDictionaryItem);
            total = dataDictionaryItemIq.Count();
            dataDictionaryItemIq = dataDictionaryItemIq.OrderBy(a => a.Sort).ThenByDescending(a => a.AddTime);
            dataDictionaryItemIq = dataDictionaryItemIq.Skip((pageIndex - 1) * pageSize).Take(pageSize);//分页前必须排序，不然EF报错

            return dataDictionaryItemIq.ToList();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idArr"></param>
        public void ListDelte(string[] idArr)
        {
            CurrentDal.Delete(a => idArr.Contains(a.DataDictionaryItemId.ToString()));
            CurrentDalSession.SaveChanges();
        }

        /// <summary>
        /// 批量禁用
        /// </summary>
        /// <param name="idArr"></param>
        public void ListForbidden(string[] idArr)
        {
            string sql = "update DataDictionaryItem set Disable=1 where DataDictionaryItemId in (@idStr)";
            string idStr = null;
            StringBuilder idStrSB = new StringBuilder("");
            foreach (var id in idArr)
            {
                idStrSB.Append("'" + id + "'");
                idStrSB.Append(",");
            }
            idStr = idStrSB.ToString();
            idStr = StringHelper.DelLastChar(idStr, ",");
            sql = sql.Replace("@idStr", idStr);
            CurrentDalSession.BaseDalExtend.ExecuteSql(sql);
        }

        /// <summary>
        /// 批量解禁
        /// </summary>
        /// <param name="idArr"></param>
        public void ListNoForbidden(string[] idArr)
        {
            string sql = "update DataDictionaryItem set Disable=0 where DataDictionaryItemId in (@idStr)";
            string idStr = null;
            StringBuilder idStrSB = new StringBuilder("");
            foreach (var id in idArr)
            {
                idStrSB.Append("'" + id + "'");
                idStrSB.Append(",");
            }
            idStr = idStrSB.ToString();
            idStr = StringHelper.DelLastChar(idStr, ",");
            sql = sql.Replace("@idStr", idStr);
            CurrentDalSession.BaseDalExtend.ExecuteSql(sql);
        }

        #region 私有方法
        /// <summary>
        /// 基础查询
        /// </summary>
        /// <param name="dataDictionaryItemIq"></param>
        /// <param name="smDataDictionaryItem"></param>
        /// <returns></returns>
        private IQueryable<DataDictionaryItem> BaseSearch(IQueryable<DataDictionaryItem> dataDictionaryItemIq, SMDataDictionaryItem smDataDictionaryItem)
        {
            if (smDataDictionaryItem.DataDictionaryHeadId.HasValue)
            {
                dataDictionaryItemIq = dataDictionaryItemIq.Where(a => a.DataDictionaryHeadId==smDataDictionaryItem.DataDictionaryHeadId.Value);
            }
            if (!string.IsNullOrEmpty(smDataDictionaryItem.Text))
            {
                dataDictionaryItemIq = dataDictionaryItemIq.Where(a => a.Text.Contains(smDataDictionaryItem.Text));
            }

            return dataDictionaryItemIq;
        }
        #endregion
    }
}
